﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SingletonDemo
{
    /// <summary>
    /// 单例模式：懒汉式，高效，线程安全
    /// 缺点：占用内存，不适合多线程环境
    /// </summary>
    public class Singleton6
    {
        private static Singleton6 singleton6 = null;

        private Singleton6()
        { }

        private static object obj = new object();

        public static Singleton6 GetInstance()
        {
            if (singleton6 == null)
            {
                lock (obj)
                {
                    if (singleton6 == null)
                    {
                        singleton6 = new Singleton6();
                    }
                }
            }
            return singleton6;
        }

        public void Test1()
        {
            Console.WriteLine("春去春回来");
        }

        public void Test2()
        {
            Console.WriteLine("花谢花又开");
        }
    }
}
